package cn.xiongxf.web.controller;

import cn.xiongxf.domain.PayOrder;
import cn.xiongxf.dto.AlipayNotifyDTO;
import cn.xiongxf.dto.PayParamDTO;
import cn.xiongxf.query.PayOrderQuery;
import cn.xiongxf.result.JsonResult;
import cn.xiongxf.result.PageList;
import cn.xiongxf.service.IPayOrderService;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/pay")
public class PayController {

    @Autowired
    public IPayOrderService payOrderService;

    /**
    * 发起支付申请
    */
    @RequestMapping(value="/apply",method= RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody @Valid PayParamDTO dto){
        String html = payOrderService.apply(dto);
        return JsonResult.success(html);
    }

    /**
     * 支付宝异步回调接口
     * 如果不需要支付宝继续调用，返回success
     */
    @RequestMapping(value="/alipay/notify",method= RequestMethod.POST)
    public String notify(AlipayNotifyDTO dto){  // 支付宝的参数不是JSON格式，不能用@RequestBody
        try {
            return payOrderService.alipayNotify(dto);
        } catch (Exception e) {  // 此处捕捉异常，避免出现全局异常
            e.printStackTrace();
            return "fail";
        }
    }

}
